package cn.uppower.wordaliy.util;

import android.support.annotation.Nullable;

import org.litepal.LitePal;

import java.nio.file.attribute.FileOwnerAttributeView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.model.FavoriteWords;
import cn.uppower.wordaliy.model.PageWord;
import cn.uppower.wordaliy.model.Words;

/**
 * Created by elitetyc on 2018/12/10.
 * Describe:
 */
public class WordsUtils {


    public static List<PageWord> getYwxyWords(@Nullable Integer size){

        if (size==null){
            size = 24;
        }

        List<PageWord> pageWords =new ArrayList<>();
        List<Words> words = LitePal.findAll(Words.class);
        for (int i=0;i<size;i++){
            List<Integer> indexs=getThreeRandomIndex(words.size(),i);
            if (LitePal.where("wordsId = ? and userPhone = ?",String.valueOf(words.get(i).getId()),WordaliyApplication.userInfo.getUserPhone()).findFirst(FavoriteWords.class)!=null){
                pageWords.add(new PageWord(words.get(indexs.get(0)).getChinese(),words.get(indexs.get(1)).getChinese(),words.get(indexs.get(2)).getChinese(),words.get(i),true));

            }else {
                pageWords.add(new PageWord(words.get(indexs.get(0)).getChinese(),words.get(indexs.get(1)).getChinese(),words.get(indexs.get(2)).getChinese(),words.get(i),false));

            }
        }
        return pageWords ;
    }


    private static List<Integer> getThreeRandomIndex(int maxIndex,int nowIndex){
        Random random=new Random();
        List<Integer> indexs=new ArrayList<>();
        int index1=0;
        do {
            index1=random.nextInt(maxIndex);
        }while (index1==nowIndex);
        indexs.add(index1);
        int index2=0;
        do {
            index2=random.nextInt(maxIndex);
        }while (index2==nowIndex||index2==indexs.get(0));
        indexs.add(index2);

        int index3=0;
        do {
            index3=random.nextInt(maxIndex);
        }while (index3==nowIndex||index3==indexs.get(0)||index3==indexs.get(1));
        indexs.add(index3);

        return indexs;
    }

    public static Object getZwxcWords() {
        List<PageWord> pageWords =new ArrayList<>();
        List<Words> words = LitePal.findAll(Words.class);
        for (int i=0;i<words.size();i++){
            List<Integer> indexs=getThreeRandomIndex(words.size(),i);

            if (LitePal.where("wordsId = ? and userPhone = ?",String.valueOf(words.get(i).getId()),WordaliyApplication.userInfo.getUserPhone()).findFirst(FavoriteWords.class)!=null){
                pageWords.add(new PageWord(words.get(indexs.get(0)).getEnglish(),words.get(indexs.get(1)).getEnglish(),words.get(indexs.get(2)).getEnglish(),words.get(i),true));

            }else {
                pageWords.add(new PageWord(words.get(indexs.get(0)).getEnglish(),words.get(indexs.get(1)).getEnglish(),words.get(indexs.get(2)).getEnglish(),words.get(i),false));


            }
        }
        return pageWords ;

    }
}
